package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
    @Autowired

    private IShoppingCartService shoppingCartService;

    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {

        //UserId不会自动获得
        Long userId = BaseContext.getThreadLocal();
        shoppingCart.setUserId(userId);
        Long dishId = shoppingCart.getDishId();

        //setmealId 和dishFlavor需要2选1
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);

        //判断当前属于套餐还是菜品

        if (dishId != null) {
            //添加到购物车的是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //添加到购物车的是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //判断是否在购物车中

        ShoppingCart one = shoppingCartService.getOne(lqw);

        if (one != null) {
            //如果已经存在，就在原来数量基础上加一
            Integer number = one.getNumber();
            one.setNumber(number + 1);
            shoppingCartService.updateById(one);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return one;


    }

    @Override
    public ShoppingCart sub(ShoppingCart shoppingCart) {
        //有两种
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();

        //添加条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        if (dishId != 0) {
            //这个是dish
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //这个是setmeal
            lqw.eq(ShoppingCart::getSetmealId, setmealId);

        }
        //判断购物车中是否还有商品
        ShoppingCart one = shoppingCartService.getOne(lqw);
        //有,-1
        Integer number = one.getNumber();

        if (number>0){
            // >0  update更新 update set number=? where id = ?
            one.setNumber(number-1);
            //更新number
            shoppingCartService.update(one,lqw);
        }else {
            // <= 0  如何操作？ delete from shopping_cart where dish_id=? and user_id = ?
            shoppingCartService.remove(lqw);

        }
        return one;



    }
}
